﻿Imports System.Data.SqlClient

Public Class frmCatalogosP
    Private ds As DataSet
    Private mstrValor As String
    Private mstrCondicion As String
    Dim mdsDatos As New DataSet
    Public ReadOnly Property Valor() As String
        Get
            Valor = mstrValor
        End Get
    End Property

    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property
    Private Sub frmCatalogosP_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.txtBuscar.Clear()
        Me.txtBuscar.Select()

        mstrValor = ""
        mstrCondicion = ""
        Dim cont As Integer = 0

        Me.dgCatalogos.Rows.Clear()

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = TextoConsulta


        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            cont = cont + 1
            Me.dgCatalogos.Rows.Add(cont, cont, rdBuscar("codigo"), rdBuscar("Nombre"))
        Loop
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Sub actualizar()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor

        Dim strNombre As String
        Dim cont As Integer = 0

        strNombre = txtBuscar.Text.Trim
        strNombre = "%" & strNombre.Replace(" ", "%") & "%"


        Me.dgCatalogos.Rows.Clear()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection
        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn

        cmdBuscar.CommandText = TextoConsulta2 & " Like '" & strNombre & "' " & mstrCondicion


        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Do While rdBuscar.Read()
            cont = cont + 1
            Me.dgCatalogos.Rows.Add(cont, cont, rdBuscar("codigo"), rdBuscar("nombre"))
        Loop


        Windows.Forms.Cursor.Current = Cursors.Default
    End Sub

    Private Sub txtBuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged
        actualizar()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub
    Private Sub dgResult_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgCatalogos.CellDoubleClick

        btnAbrir_Click(Me, System.EventArgs.Empty)

    End Sub

    Private Sub dgResult_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgCatalogos.KeyDown

        If e.KeyCode = Keys.Enter Then
            btnAbrir_Click(Me, System.EventArgs.Empty)
        End If

    End Sub

    Private Sub RadButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton2.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnAbrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbrir.Click
        Dim strRetVal As String = ""

        Try
            strRetVal = CStr(dgCatalogos.Item(0, dgCatalogos.CurrentCell.RowIndex).Value)
        Catch ex As Exception
            strRetVal = ""
        Finally
            mstrValor = strRetVal.Trim
        End Try

        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
End Class